/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.service.services.views.orgchart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;


/**
 * This class implements an organization chart line, for tree and semalike
 *  orgcharts.
 * It should be on top of each son cell of the organization chart
 */
public class OrgLine1 extends JComponent
{
    /** Kind of line. */
    private short position;

    /** Background color. */
    private Color backColor = null;

    /** Line height. */
    private int height = OrgChartPanel.SPACEV;

    /** The corresponding cell is the last of the siblings or not. */
    private boolean last = false;

    /**
     * Creates a new OrgLine1 object.
     *
     * @param abackColor  Background color.
     */
    public OrgLine1(final Color abackColor)
    {
        super();
        this.position = OrgChartPanel.VERTICAL;
        this.backColor = abackColor;
    }

    /**
     * Creates a new OrgLine1 object.
     *
     * @param aposition  Kind of line.
     * @param abackColor  Background color.
     * @param aheight  Line height.
     */
    public OrgLine1(final short aposition, final Color abackColor,
        final int aheight)
    {
        super();
        this.position = aposition;
        this.backColor = abackColor;
        this.height = aheight;
    }

    /**
     * Creates a new OrgLine1 object.
     *
     * @param aposition  Kind of line.
     * @param abackColor  Background color.
     * @param aheight  Line height.
     * @param alast  Last cell flag.
     */
    public OrgLine1(final short aposition, final Color abackColor,
        final int aheight, final boolean alast)
    {
        super();
        this.position = aposition;
        this.backColor = abackColor;
        this.height = aheight;
        this.last = alast;
    }

    /**
     * Gets minimum size of the component.
     *
     * @return The minimum size according to the line height.
     */
    @Override
	public final Dimension getMinimumSize()
    {
        return new Dimension(OrgChartPanel.SPACEH, height);
    }

    /**
     * Gets preferred size of the component, which is the minimum size.
     *
     * @return The preferred size.
     */
    @Override
	public final Dimension getPreferredSize()
    {
        return getMinimumSize();
    }

    /**
     * Gets maximum size of the component, which is the minimum size.
     *
     * @return The maximum size.
     */
    @Override
	public final Dimension getMaximumSize()
    {
        return getMinimumSize();
    }

    /**
     * Draws the component.
     *
     * @param g  Graphics on which to draw the component.
     */
    @Override
	public final void paint(final Graphics g)
    {
        int w = getSize().width;
        int h = getSize().height;

        // background color
        if (backColor != null)
        {
            g.setColor(backColor);
            g.fillRect(0, 0, w, h);
        }

        // lines
        g.setColor(Color.black);

        if (position == OrgChartPanel.VERTICAL)
        {
            g.drawLine(w / 2, 0, w / 2, h);
            g.drawLine((w / 2) + 1, 0, (w / 2) + 1, h);
        }
        else if (position == OrgChartPanel.HORIZONTAL_LEFT)
        {
            g.drawLine(w / 2, 0, w / 2, h / 2);
            g.drawLine(w / 2, h / 2, w, h / 2);
            g.drawLine((w / 2) + 1, 0, (w / 2) + 1, h / 2);
            g.drawLine(w / 2, (h / 2) + 1, w, (h / 2) + 1);

            if (!last)
            {
                g.drawLine(w / 2, h / 2, w / 2, h);
                g.drawLine((w / 2) + 1, h / 2, (w / 2) + 1, h);
            }
        }
    }
}
